package com.cheyun.netsalev3.widget

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.util.Log
import android.view.*
import android.widget.SeekBar
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.databinding.CallPopLayoutBinding
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.CallPopFragmentViewModel


class CallPopFragment : DialogFragment() {
    lateinit var binding: CallPopLayoutBinding
    var url = ""
    var starttime = ""
    var timesName = ""
    /**系统调用这个来获得对话片段的布局，不管是否显示为对话框或嵌入片段。*/
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        //将布局膨胀为对话框或嵌入片段
        binding = DataBindingUtil.inflate(inflater, R.layout.call_pop_layout, container, false)
        var factory = InjectorUtils.getCallPopFragmentViewModelFactory(url, starttime, timesName)
        var viewModel = ViewModelProviders.of(this, factory).get(CallPopFragmentViewModel::class.java)
        binding.viewModel = viewModel
        initView()
        binding.setLifecycleOwner(this)
        return binding.root
    }

    //是否为用户点击
    var mUserIsSeeking = false

    private fun initView() {
        binding.closBut.setOnClickListener {
            dismiss()
        }
        binding.viewModel?.durationTime?.observe(this, Observer {
            Log.e("durationTime",it.toString())
            binding.mSeekbarAudio.max = it.toInt()
        })

        binding.viewModel?.currentime?.observe(this, Observer {
            binding.mSeekbarAudio.progress = it
        })

        binding.mSeekbarAudio.setOnSeekBarChangeListener(
                object : SeekBar.OnSeekBarChangeListener {
                    var userSelectedPosition = 0

                    override fun onStartTrackingTouch(seekBar: SeekBar) {
                        mUserIsSeeking = true
                    }

                    override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                        if (fromUser) {
                            Log.e("onProgressChanged",progress.toString())
                            userSelectedPosition = progress
                        }
                    }

                    override fun onStopTrackingTouch(seekBar: SeekBar) {
                        mUserIsSeeking = false
                        Log.e("userSelectedPosition",userSelectedPosition.toString())
                        binding.viewModel?.seekTo(userSelectedPosition)
                    }
                })

    }


    /**系统仅在对话框中创建布局时调用此操作。*/
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        //外部点击不消失
        dialog.setCancelable(false)
        dialog.setCanceledOnTouchOutside(false)
        dialog.setOnKeyListener(object : DialogInterface.OnKeyListener {
            override fun onKey(dialog: DialogInterface, keyCode: Int, event: KeyEvent): Boolean {
                return keyCode == KeyEvent.KEYCODE_BACK
            }
        })

        val window = dialog.window
        window.setBackgroundDrawableResource(android.R.color.transparent)
        return dialog
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            url = it.getString("url")
            starttime = it.getString("starttime")
            timesName = it.getString("timesName")
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(url: String, starttime: String, timesName: String) =
                CallPopFragment().apply {
                    arguments = Bundle().apply {
                        putString("url", url)
                        putString("starttime", starttime)
                        putString("timesName", timesName)
                    }
                }
    }


}